package com.tiyunge.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.tiyunge.system.domain.SysGradeOption;
import com.tiyunge.system.service.ISysGradeOptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.tiyunge.common.annotation.Log;
import com.tiyunge.common.core.controller.BaseController;
import com.tiyunge.common.core.domain.AjaxResult;
import com.tiyunge.common.core.page.TableDataInfo;
import com.tiyunge.common.enums.BusinessType;
import com.tiyunge.common.utils.poi.ExcelUtil;
/**
 * 年级选项 信息操作处理
 *
 * @author yejiayu
 */
@RestController
@RequestMapping("/system/gradeOption")
public class SysGradeOptionController extends BaseController
{
    @Autowired
    private ISysGradeOptionService gradeOptionService;

    /**
     * 获取年级选项列表
     */
    @PreAuthorize("@ss.hasPermi('content:gradeOption:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysGradeOption gradeOption)
    {
        startPage();
        List<SysGradeOption> list = gradeOptionService.selectGradeOptionList(gradeOption);
        return getDataTable(list);
    }

    /**
     * 导出年级选项列表
     */
    @Log(title = "年级选项管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('content:gradeOption:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysGradeOption gradeOption)
    {
        List<SysGradeOption> list = gradeOptionService.selectGradeOptionList(gradeOption);
        ExcelUtil<SysGradeOption> util = new ExcelUtil<SysGradeOption>(SysGradeOption.class);
        util.exportExcel(response, list, "年级选项数据");
    }

    /**
     * 根据年级选项编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('content:gradeOption:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id)
    {
        return success(gradeOptionService.selectGradeOptionById(id));
    }

    /**
     * 新增年级选项
     */
    @PreAuthorize("@ss.hasPermi('content:gradeOption:add')")
    @Log(title = "年级选项管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysGradeOption gradeOption)
    {
        if (!gradeOptionService.checkGradeCodeUnique(gradeOption))
        {
            return error("新增年级选项'" + gradeOption.getGradeName() + "'失败，年级编码已存在");
        }
        gradeOption.setCreateBy(getUsername());
        return toAjax(gradeOptionService.insertGradeOption(gradeOption));
    }

    /**
     * 修改年级选项
     */
    @PreAuthorize("@ss.hasPermi('content:gradeOption:edit')")
    @Log(title = "年级选项管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysGradeOption gradeOption)
    {
        if (!gradeOptionService.checkGradeCodeUnique(gradeOption))
        {
            return error("修改年级选项'" + gradeOption.getGradeName() + "'失败，年级编码已存在");
        }
        gradeOption.setUpdateBy(getUsername());
        return toAjax(gradeOptionService.updateGradeOption(gradeOption));
    }

    /**
     * 删除年级选项
     */
    @PreAuthorize("@ss.hasPermi('content:gradeOption:remove')")
    @Log(title = "年级选项管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        gradeOptionService.deleteGradeOptionByIds(ids);
        return success();
    }
}